//
//  UIViewController+DLExtension.m
//  dielianw
//
//  Created by 刘威 on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "UIViewController+DLExtension.h"
#import <objc/runtime.h>

@implementation UIViewController (DLExtension)

static char *navBarHairlineKey = "navBarHairlineImage";

- (void)createNavigationBackItem {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationButtonReturnBlack.png"] forState:UIControlStateNormal];
    backBtn.frame =CGRectMake(0, 0, backBtn.currentBackgroundImage.size.width, backBtn.currentBackgroundImage.size.height);
    backBtn.exclusiveTouch = YES;
    backBtn.tintColor = self.navigationController.navigationBar.tintColor;
    [backBtn addTarget:self action:@selector(actionBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
}

- (void)createLeftTwoNavigationItem:(UIImage *)image {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationButtonReturnBlack.png"] forState:UIControlStateNormal];
    backBtn.frame =CGRectMake(0, 0, backBtn.currentBackgroundImage.size.width, backBtn.currentBackgroundImage.size.height);
    backBtn.exclusiveTouch = YES;
    backBtn.tintColor = self.navigationController.navigationBar.tintColor;
    [backBtn addTarget:self action:@selector(actionBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
//    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    
    UIButton *backBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn3.exclusiveTouch = YES;
    [backBtn3 setBackgroundImage:image forState:UIControlStateNormal];
    backBtn3.frame =CGRectMake(0, 0, backBtn3.currentBackgroundImage.size.width / 3.0f, backBtn3.currentBackgroundImage.size.height);
    backBtn3.exclusiveTouch = YES;
    backBtn3.hidden = YES;
    backBtn3.tintColor = self.navigationController.navigationBar.tintColor;
//    [backBtn3 addTarget:self action:@selector(actionLeftTwoItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:backBtn3];
    
    
    UIButton *backBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn2.exclusiveTouch = YES;
    [backBtn2 setBackgroundImage:image forState:UIControlStateNormal];
    backBtn2.frame =CGRectMake(0, 0, backBtn2.currentBackgroundImage.size.width, backBtn2.currentBackgroundImage.size.height);
    backBtn2.exclusiveTouch = YES;
    backBtn2.tintColor = self.navigationController.navigationBar.tintColor;
    [backBtn2 addTarget:self action:@selector(actionLeftTwoItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:backBtn2];
//    self.navigationItem.leftBarButtonItem = leftButtonItem2;
    
    [self.navigationItem setLeftBarButtonItems:@[leftButtonItem,leftButtonItem3,leftButtonItem2]];
}

- (void)createNavigationBackItem:(UIImage *)image {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.exclusiveTouch = YES;
    [backBtn setBackgroundImage:image forState:UIControlStateNormal];
    backBtn.frame =CGRectMake(0, 0, backBtn.currentBackgroundImage.size.width, backBtn.currentBackgroundImage.size.height);
    backBtn.exclusiveTouch = YES;
    backBtn.tintColor = self.navigationController.navigationBar.tintColor;
    [backBtn addTarget:self action:@selector(actionBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
}


- (void)createNavigationRightImageItem:(UIImage *)image {
    if (image == nil) {
        self.navigationItem.rightBarButtonItem = nil;
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, button.currentBackgroundImage.size.width, button.currentBackgroundImage.size.height);
    button.tintColor = self.navigationController.navigationBar.tintColor;
    [button addTarget:self action:@selector(actionNavigationRightItem:)];
    button.exclusiveTouch = YES;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)createNavigationRightTitleItem:(NSString *)title color:(UIColor *)color {

    if (title == nil) {
        self.navigationItem.rightBarButtonItem = nil;
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [self getSize:title];
    button.frame = CGRectMake(0, 0, size.width, 40);
    [button setTitle:title];
    button.titleLabel.font = [UIFont nameFont];
    [button setTitleColor:color];
    [button addTarget:self action:@selector(actionNavigationRightItem:)];
    button.exclusiveTouch = YES;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)createNavigationLeftTitleItem:(NSString *)title color:(UIColor *)color {
    
    
    if (title == nil) {
        self.navigationItem.leftBarButtonItem = nil;
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [self getSize:title];
    button.frame = CGRectMake(0, 0, size.width, 40);
    [button setTitle:title];
    button.titleLabel.font = [UIFont nameFont];
    [button setTitleColor:color];
    [button addTarget:self action:@selector(actionNavigationLeftItem:)];
    button.exclusiveTouch = YES;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (CGSize)getSize:(NSString *)title {
    CGSize textSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName:[UIFont nameFont]}
                                         context:nil].size;
    return textSize;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)setNavBarHairlineImageViewHidden:(BOOL)hidden {
    [self findHairlineImageViewUnder:self.navigationController.navigationBar].hidden = hidden;
}

- (void)setNavigationBarColorRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)alpha {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:alpha ]] forBarMetrics:UIBarMetricsDefault];
}

- (void)setNavBarHairlineImageView:(UIImageView *)navBarHairlineImageView {
    
    objc_setAssociatedObject(self, navBarHairlineKey, navBarHairlineImageView, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIImageView *)navBarHairlineImageView {
    return objc_getAssociatedObject(self, navBarHairlineKey);
}
-(void)setNavBarTitleColor:(UIColor *)color
{
    NSMutableDictionary  * textAtts = [NSMutableDictionary dictionary];
    textAtts[NSForegroundColorAttributeName]= color;//设置文字颜色
    textAtts[NSFontAttributeName] = [UIFont navControllerTitleFont];//设置文字大小
    [self.navigationController.navigationBar setTitleTextAttributes:textAtts];
}

- (void)setRightItemTitleColor:(UIColor *)color{
    NSMutableDictionary  * textAtts = [NSMutableDictionary dictionary];
    textAtts[NSForegroundColorAttributeName]= color;//设置文字颜色
    textAtts[NSFontAttributeName] = [UIFont systemFontOfSize:15];//设置文字大小
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:textAtts forState:UIControlStateNormal];
}


- (void)startLoading {
    UIView *loadingView = [self.view viewWithTag:999999];
    if (!loadingView) {
        loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        loadingView.tag = 999999;
        loadingView.backgroundColor = [UIColor whiteColor];
        UIImage *image = [UIImage imageNamed:@"loading1"];
        UIImage *image2 = [UIImage imageNamed:@"loading2"];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - image.size.width) / 2.0f, (SCREEN_HEIGHT - image.size.height) / 2.5f, image.size.width, image.size.height)];
        imageView.image = image;
        imageView.animationImages = @[image,image2];
        [loadingView addSubview:imageView];
        imageView.animationDuration = 0.2f;
        imageView.tag = 999998;
        [self.view addSubview:loadingView];
        [self.view bringSubviewToFront:loadingView];
    }
    UIImageView *imageView = [loadingView viewWithTag:999998];
    [imageView startAnimating];
}

- (void)stopLoading {
    [self performSelector:@selector(stopLoad) withObject:nil afterDelay:0.4f];
}

- (void)stopLoad {
    UIView *loadingView = [self.view viewWithTag:999999];
    if (loadingView) {
        UIImageView *imageView = [loadingView viewWithTag:999998];
        if (imageView) {
            [imageView stopAnimating];
            [imageView removeFromSuperview];
            imageView = nil;
        }
        [loadingView removeFromSuperview];
        loadingView = nil;
    }
}

@end
